-
Notifications
You must be signed in to change notification settings - Fork 202
Implement unused assignments lints on HIR #4285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+144
−96
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4712032 to
344b6eb
Compare
CohenArthur
reviewed
Nov 14, 2025
Member
CohenArthur
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really good work, well done!!!
95fc4c2 to
9353bdf
Compare
P-E-P
requested changes
Nov 24, 2025
02a2dd5 to
6f54a63
Compare
9b3255d to
044e865
Compare
gcc/rust/ChangeLog: * checks/lints/unused-var/rust-unused-var-checker.cc (UnusedVarChecker): Implement unused assignments warning. (UnusedVarChecker::go): Remove unique pointer unused var context. (UnusedVarChecker::visit): Visit AssignExpr in HIR default visitor. * checks/lints/unused-var/rust-unused-var-checker.h: Add visit method. * checks/lints/unused-var/rust-unused-var-collector.cc (UnusedVarCollector): Collect warnings for assignments. (UnusedVarCollector::visit): Visit AssignExpr in HIR default visitor. * checks/lints/unused-var/rust-unused-var-collector.h: Add visit method. * checks/lints/unused-var/rust-unused-var-context.cc (UnusedVarContext::add_assign): Add assignment in map. (UnusedVarContext::remove_assign): Remove assignment in map. (UnusedVarContext::is_variable_assigned): Check if a variable is assigned. * checks/lints/unused-var/rust-unused-var-context.h: Add a map to stock assignments. gcc/testsuite/ChangeLog: * rust/compile/issue-4260_0.rs: New test. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
gcc/rust/ChangeLog: * checks/lints/unused-var/rust-unused-var-checker.cc (UnusedVarChecker::visit): Change unused name warning to unused variable warning. * checks/lints/unused-var/rust-unused-var-collector.cc (UnusedVarCollector::visit): Remove useless methods. * checks/lints/unused-var/rust-unused-var-collector.h: Same here. * checks/lints/unused-var/rust-unused-var-context.cc (UnusedVarContext::add_variable): Add used variables to set. (UnusedVarContext::mark_used): Remove method. (UnusedVarContext::is_variable_used): Check if the set contains the hir id linked to a variable. (UnusedVarContext::as_string): Refactor method for new set. * checks/lints/unused-var/rust-unused-var-context.h: Refactor methods. * lang.opt: Change description for unused check flag. gcc/testsuite/ChangeLog: * rust/compile/static_item_0.rs: Modify warning output. * rust/compile/template_function_0.rs: Modify warning output. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
gcc/rust/ChangeLog: * Make-lang.in: Compile the right files. * checks/lints/unused-var/rust-unused-var-checker.cc: Move to... * checks/lints/unused/rust-unused-checker.cc: ...here. * checks/lints/unused-var/rust-unused-var-checker.h: Move to... * checks/lints/unused/rust-unused-checker.h: ...here. * checks/lints/unused-var/rust-unused-var-collector.cc: Move to... * checks/lints/unused/rust-unused-collector.cc: ...here. * checks/lints/unused-var/rust-unused-var-collector.h: Move to... * checks/lints/unused/rust-unused-collector.h: ...here. * checks/lints/unused-var/rust-unused-var-context.cc: Move to... * checks/lints/unused/rust-unused-context.cc: ...here. * checks/lints/unused-var/rust-unused-var-context.h: Move to... * checks/lints/unused/rust-unused-context.h: ...here. * rust-session-manager.cc (Session::compile_crate): Call the right method. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
044e865 to
10403d9
Compare
P-E-P
approved these changes
Dec 18, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4260
on top of PR #4283 and PR #4331
Used the HIR default visitor for AssignExpr, added a map, to have all the unused assignments and attached some methods to it.
Since
is an HIR::PathInExpression we cannot visit the left expr of an assignment, because it would be considerered used. So I also added a walk_right to only visit the right expr.
( if we have a = b, we want to visit b)